<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - interactive cubes</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="js/libs/jquery-1.6.4.js"></script>
		<script src="js/libs/three.js"></script>
		<script src="js/loaders/OBJLoader.js"></script>
		
		<script>

			var container, stats;
			var camera, scene, projector, raycaster, renderer;

			var mouse = new THREE.Vector2(), INTERSECTED;
			var radius = 100, theta = 0;

			var selectedContent;
			var chair;
			var table;
			var r=0;
			
			init();
			animate();

			function init() {
				container = document.createElement( 'div' );
				container.setAttribute("style", "background:url(assets/gallery01.jpg) no-repeat");
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );

				scene = new THREE.Scene();

				var light = new THREE.DirectionalLight( 0xffffff, 2 );
				light.position.set( 1, 1, 1 ).normalize();
				scene.add( light );

				var light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( -1, -1, -1 ).normalize();
				scene.add( light );

				table = loadObj("3");
				chair = loadObj("2");

				projector = new THREE.Projector();
				raycaster = new THREE.Raycaster();

				renderer = new THREE.WebGLRenderer();
				renderer.sortObjects = false;
				renderer.setSize( window.innerWidth, window.innerHeight );

				container.appendChild(renderer.domElement);

				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
				document.addEventListener( 'mousedown', onDocumentMouseDown, false );
				document.addEventListener('keydown', onDocumentKeyDown, false);
				
				window.addEventListener( 'resize', onWindowResize, false );
			}
			function onDocumentKeyDown(event){
				//for rotation
				//selectedContent.rotation.setY(20)
				//treeMesh.position.set( 0, -150, -150 );
				//treeMesh.scale.set( sc, sc, sc );				
				var step = 50;
				
				switch( event.keyCode ) {
					case 37:{//left
						console.log("left");
						selectedContent.position.x += step;
						break;
					}case 38:{//up
						console.log("up");
						selectedContent.position.z += step;
						break;
					}case 39:{//right
						console.log("right");
						selectedContent.position.x -= step;
						break;
					}case 40:{//down
						console.log("down");
						selectedContent.position.z -= step;
						break;
					}case 69:{//e
						selectedContent.position.y += step;
						break;
					}case 81:{//q
						selectedContent.position.y -= step;
						break;
					}case 82:{//r
						r+=step;
						selectedContent.rotation.setY(r);
						break;
					}
					
				}
			}

			function loadObj(id){
				var texture = new THREE.Texture();
			
				var loader = new THREE.ImageLoader();
				loader.addEventListener( 'load', function ( event ) {
					texture.image = event.content;
					texture.needsUpdate = true;
				});
				loader.load( 'assets/' + id + '/' + id + '.png' );

				// model

				var loader = new THREE.OBJLoader();
				loader.addEventListener( 'load', function ( event ) {

					var object = event.content;

					object.traverse( function ( child ) {
						if ( child instanceof THREE.Mesh ) {
							child.material.map = texture;
							content = child;
						}
					} );
					object.position.y = - 160;
					scene.add( object );
					return object; 
				}
					
				);
				loader.load( 'assets/' + id + '/' + id + '.obj');
			}
			
			function onWindowResize() {
				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );
			}

			function onDocumentMouseMove( event ) {
				event.preventDefault();

				mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
				mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
			}
			
			function onDocumentMouseDown(event){
				var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
				projector.unprojectVector( vector, camera );

				raycaster.set( camera.position, vector.sub( camera.position ).normalize() );

				var intersects = raycaster.intersectObjects( scene.children, true );

				if ( intersects.length > 0 ) {
					//change material
					//var materialLines = new THREE.MeshBasicMaterial( { wireframe: true } );
					//content.material  = materialLines;
					console.log("selected");
					selectedContent = intersects[0].object; 
				} 
			}

			function animate() {
				requestAnimationFrame( animate );
				render();
			}

			function render() {
				camera.position.z = 300;
				renderer.render( scene, camera );
			}

		</script>
	</body>
</html>
